package vn.sunnet.backviet01.game.coloa.human.enemy;

import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.modifier.ease.EaseBackOut;

public class EnemyArrow extends Sprite{
	public PhysicsHandler handler;
	public RotationModifier rotation;
	
	private byte turnUpdate = -1;
	
	private final int kind;
	
	public EnemyArrow(final int kind, float pX, float pY, TextureRegion pTextureRegion) {
		super(pX, pY, pTextureRegion);
		this.kind = kind;
		
		reset();
	}

	public void reset() {
		super.reset();
		handler = new PhysicsHandler(this);
		if (kind == 0) {
			rotation = new RotationModifier(2f, 20, -4, EaseBackOut.getInstance());
			registerEntityModifier(rotation);
			registerUpdateHandler(handler);
		} else {
		}
		turnUpdate = 0;
	}
	
	public void setPosition(float pX, float pY) {
		super.setPosition(pX, pY);
		if (kind == 1) {
			RotationModifier modifier = new RotationModifier(1f, -5, 20);
			setRotationCenter(70, 6);
			registerEntityModifier(modifier);
			modifier.setRemoveWhenFinished(true);
		}	
	}
	
	public void flying() {
		rotation = new RotationModifier(2.5f, 20, -4, EaseBackOut.getInstance());
		registerEntityModifier(rotation);
		registerUpdateHandler(handler);
	}
	
	public void updateNormal() {
		if (this.getX() <= 420 && this.getX() >= 350)
			turnUpdate = 1;
		else if (this.getX() < 350 && this.getX() >= 310)
			turnUpdate = 2;
		else if (this.getX() < 310 && this.getX() >= 280)
			turnUpdate = 3;
		else if (this.getX() < 280 && this.getX() >= 190)
			turnUpdate = 4;
		
		
		if (turnUpdate == 0) {
			this.handler.setVelocity( -600, -150);
		} else if (turnUpdate == 1) {
			this.handler.setVelocity(-600, -90);
		} else if (turnUpdate == 2) {
			this.handler.setVelocity(-600, -30);
		}else if (turnUpdate == 3) {
			this.handler.setVelocity(-600, -7);
		}else if (turnUpdate == 4) {
			this.handler.setVelocity(-600, 0);
		}
	}
	
	public void update() {
		if (this.getX() <= 420 && this.getX() >= 290)
			turnUpdate = 1;
		else if (this.getX() <= 350 && this.getX() >= 310)
			turnUpdate = 2;
		else if (this.getX() <= 310 && this.getX() >= 280)
			turnUpdate = 3;
		else if (this.getX() <= 280 && this.getX() >= 190)
			turnUpdate = 4;
		
		
		if (turnUpdate == 0) {
			this.handler.setVelocity( -800, -150);
		} else if (turnUpdate == 1) {
			this.handler.setVelocity(-800, -90);
		} else if (turnUpdate == 2) {
			this.handler.setVelocity(-800, -30);
		}else if (turnUpdate == 3) {
			this.handler.setVelocity(-800, -7);
		}else if (turnUpdate == 4) {
			this.handler.setVelocity(-800, 0);
		}
		
	}
	
	public void onManagedUpdate(float pSecondsElapsed) {
		if (this.kind == 0) {
			updateNormal();
		} else 
			update();
		
		super.onManagedUpdate(pSecondsElapsed);
	}
	
}
